package cn.topcodes.tcsf.wechat.web.interceptor;

import cn.topcodes.tcsf.wechat.service.WechatMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Unicorn on 2018/3/20.
 */
public class CurrentMpInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private WechatMpService wechatMpService;

    /**
     * This implementation always returns {@code true}.
     *
     * @param request
     * @param response
     * @param handler
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return super.preHandle(request, response, handler);
    }

    /**
     * This implementation is empty.
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if(modelAndView != null && modelAndView.getModel() != null) {
            modelAndView.getModel().put("currentMpId", wechatMpService.getCurrentMpId());
            modelAndView.getModel().put("currentWechatMpId", wechatMpService.getCurrentWechatMpId());
        }
        super.postHandle(request, response, handler, modelAndView);
    }
}
